#include "link.h"

// 创建链表
void link_create(link **L)
{
    *L = (link *)malloc(sizeof(link));
    (*L)->prev = *L;
    (*L)->next = *L;
    (*L)->data = 0;
}

// 插入节点
int node_insert(link *L, int pos, int data)
{
    assert(NULL != L);

    link *new = (link *)malloc(sizeof(link));
    new->data = data;

    // 头部插入
    // new->next = L->next;
    // new->prev = L;
    // 尾部插入
    new->next = L;
    new->prev = L->prev;
    new->next->prev = new;
    new->prev->next = new;
    
    return 0;
}

int node_delete(link *L)
{
    return 0;
}

int link_show(link *L)
{
    printf("%16s %14s %16s %4s\n", "<-prev", "self", "next->", "data");

    if (NULL == L || L->next == L || L->prev ==L) {
        printf("列表为空\n");
        return -1;
    }

    link *p = L;
    while(p->next != L) {

        printf("<-%14p %14p %14p-> %4d\n",p->prev, p, p->next,  p->data);

        p = p->next;
    }

    printf("<-%14p %14p %14p-> %4d\n",p->prev, p, p->next,  p->data);

    return 0;
}

// 释放链表
int link_free(link **L)
{
    link *cur = (*L)->next;
    link *next;

    while(next != (*L)) {
        next = cur->next;
        free(cur);
        cur = next;
    }
    
    free(*L);
    *L = NULL;

    return 0;
}

int search_by_pos(link *L, int pos)
{
    return 0;
}

int search_by_data(link *L, int data)
{
    return 0;
}

int update_by_pos(link *L, int pos)
{
    return 0;
}

int update_by_data(link *L, int data)
{
    return 0;
}

